<template>
  <el-form label-width="70px" :rules="rules" :model="phone" ref="formRef">
    <el-form-item label="手机号" prop="num">
      <el-input v-model="phone.num" />
    </el-form-item>
    <el-form-item label="验证码" prop="code">
      <div class="get-code">
        <el-input v-model="phone.code" />
        <el-button type="primary" class="get-btn" @click="getCode"
          >获取验证码</el-button
        >
      </div>
    </el-form-item>
  </el-form>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from "vue"
import { rules } from "../config/phone-config"
import { ElForm } from "element-plus"
import { loginStore } from "@/stores/modules/login"

export default defineComponent({
  setup() {
    const phone = reactive({
      num: "",
      code: ""
    })

    const formRef = ref<InstanceType<typeof ElForm>>()

    const LS = loginStore()

    const getCode = () => {
      phone.code = "123456"
    }

    // 登录
    const loginAction = () => {
      formRef.value?.validate(valid => {
        if (valid) {
          const { num, code } = phone
          LS.Login({ username: num, password: code })
        }
      })
    }

    return {
      phone,
      rules,
      formRef,
      loginAction,
      getCode
    }
  }
})
</script>

<style scoped>
.get-code {
  display: flex;
}

.get-btn {
  margin-left: 8px;
}
</style>
